package com.espirit.hr;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.Index;
import org.hibernate.annotations.Table;

import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.manager.Department;
import com.espirit.eap.sa.Employee;
import com.espirit.eap.sa.User;
import com.espirit.finance.core.AcctTerm;
import com.espirit.finance.core.AcctTermCheckAware;

@Entity
@DomainModel
@Table(indexes={
		@Index(name="idx_empdate", columnNames={"employee_id","realdate"} )},appliesTo = "espirit_deliverytimeitem")
public class DeliveryTimeItem implements Serializable, EmployeeAware, VersionAware, AcctTermCheckAware{
	private static final long serialVersionUID = 2762525961758963267L;
	private Integer id;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	

	@ManyToOne(fetch=FetchType.LAZY)
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}

	public Date getCreatedOn() {
		return createdOn;
	}
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Integer getTimes() {
		return times;
	}
	public void setTimes(Integer times) {
		this.times = times;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public User getCreated() {
		return created;
	}
	public void setCreated(User created) {
		this.created = created;
	}
	
	public Date getRealDate() {
		return realDate;
	}
	public void setRealDate(Date realDate) {
		this.realDate = realDate;
	}

	@Transient
	public String getDeptIds() {
		return deptIds;
	}
	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}
	
	@Override
	public boolean check(AcctTerm acctTerm) {
		if (acctTerm != null && acctTerm.getBeginDate() != null && this.realDate != null){
			return acctTerm.getBeginDate().compareTo(this.realDate) < 0;
		} else {
			return true;
		}
	}

	private Integer times;
	private Employee employee;
	private Department department;
	private String remark;
	private Date createdOn;
	private User created;
	private Date realDate;
	private String deptIds;

}
